package com.sphd.emanu.fragment.goodorderdetail;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sphd.emanu.R;
import com.sphd.emanu.activity.ordersdetail.OrdersdetailActivity;
import com.sphd.emanu.adapter.GoodOrderDetailAdapter;
import com.sphd.emanu.config.Constant;
import com.sphd.emanu.interfaces.OrderCountCallBack;
import com.sphd.emanu.interfaces.SwfInterface;
import com.sphd.emanu.mvp.MVPBaseFragment;
import com.sphd.emanu.permission.Moudle;
import com.sphd.emanu.pojo.login.Org;
import com.sphd.emanu.pojo.login.User;
import com.sphd.emanu.pojo.order.Order;
import com.sphd.emanu.utils.PermissionConfig;
import com.wzutils.L;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @Author Hu
 * @Time 2017/7/4 -- 17:27.
 * @Email 87683202@qq.com
 */
public class GoodorderdetailFragment extends MVPBaseFragment<GoodorderdetailContract.View, GoodorderdetailPresenter> implements GoodorderdetailContract.View, SwipeRefreshLayout.OnRefreshListener {
    int flag, type;//页面标志/状态标志
    Bundle bundle;
    User user;
    @BindView(R.id.my_recycler)
    RecyclerView myRecycler;
    @BindView(R.id.my_swf)
    SwipeRefreshLayout mySwf;
    Unbinder unbinder;
    GoodOrderDetailAdapter goodadapter;
    OrderCountCallBack orderCountCallBack;
    List<Moudle> moudles;
    boolean isCanDo = false;
    SwfInterface swfInterface;

    public void setSwfInterface(SwfInterface swfInterface) {
        this.swfInterface = swfInterface;
    }

    public void setOrderCountCallBack(OrderCountCallBack orderCountCallBack) {
        this.orderCountCallBack = orderCountCallBack;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_sale_topup_order;
    }

    @Override
    protected void initView() {
        unbinder = ButterKnife.bind(this, rootView);
        bundle = getArguments();
        user = (User) bundle.getSerializable(Constant.USER_ENTITY);
        moudles = (List<Moudle>) bundle.getSerializable(Constant.PERMISSION_LIST);
        isCanDo = isCanDo(moudles, PermissionConfig.MENU_RECEIVE_2);

        initEvent();
        initAdapter();
        setUserVisibleHint(true);
    }

    private void initEvent() {
        mySwf.setOnRefreshListener(this);
        myRecycler.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                Order order = (Order) adapter.getItem(position);
                bundle.putSerializable(Constant.ORDER_ENTITY, order);
                Intent intent = new Intent(context, OrdersdetailActivity.class);
                intent.putExtras(bundle);
                startActivityForResult(intent, Constant.REQUEST_CODE);

            }

            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                super.onItemChildClick(adapter, view, position);
                /**
                 status订单状态,0-订单取消,1-订单提交,2-订单接受,3-订单拒绝,4-订单派送,5-订单完成,9-过期订单
                 原型上的状态顺序为12450
                 原型上的取消按钮变为拒绝,
                 修改订单状态时,要变成啥状态就传对应的状态值,比如,要接受,就传2,要取消,就传0
                 */
                if (isCanDo) {
                    //// TODO: 2017/8/3 待有订单的时候处理
                    Order order = (Order) adapter.getItem(position);
                    switch (view.getId()) {
                        case R.id.item_btn_rec:
                            //订单接收
                            changeStatus(order, 2);
                            break;
                        case R.id.item_btn_cancle:
                            //订单拒绝即为取消
                            changeStatus(order, 0);
                            break;
                        case R.id.item_send:
                            //派送
                            changeStatus(order, 4);
                            break;
                        case R.id.item_pay_bill:
                            //结账
                            changeStatus(order, 5);
                            break;
                        case R.id.item_print_p:
                            //打印小票
                            toast("暂未开通");
                            break;
                    }


                } else {
                    toast(getString(R.string.no_perssion));
                }
            }
        });
    }

    private void changeStatus(Order order, int state) {
        showProgressDialog();
        mPresenter.updateOrderStatus(order.getId(), state);
    }

    private void initAdapter() {
        setRefresh(mySwf);
        setRecyWithDecorationBySelf(myRecycler, R.color.red, R.dimen.dp_0_5);
        goodadapter = new GoodOrderDetailAdapter(R.layout.item_good_order_detail, new ArrayList<Order>(), context, flag);
        goodadapter.setEmptyView(emptyView);
        myRecycler.setAdapter(goodadapter);
    }

    @Override
    protected void initData() {
        showProgressDialog();
        mPresenter.selectOrder(user.getOid(), String.valueOf(type));
    }


    public void setType(int flag, int type) {
        this.flag = flag;
        this.type = type;
    }

    @Override
    public void recBean(Org org) {
        dissProgressDialog();
        orderCountCallBack.callBackOrderCount(org.getOrderCount());
        goodadapter.setNewData(org.getOrdersList());
    }

    @Override
    public void recRes(int res, int i) {
        dissProgressDialog();
        if (res == 1) {
            toast(getString(R.string.up_success));
            if (i == 2) {
                swfInterface.swf(1);
            } else if (i == 0) {
                swfInterface.swf(4);
            } else if (i == 4) {
                swfInterface.swf(2);
            } else if (i == 5) {
                swfInterface.swf(3);
            }


        } else {
            toast(getString(R.string.up_failure));

        }

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onRefresh() {
        initData();
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == Constant.REQUEST_CODE) {
            onRefresh();
            //下面这个开启后报错 暂时不能实现
//            swfInterface.swf(data.getIntExtra(Constant.FLAG, 0));
        }
    }
}
